package br.com.extrajudicial.dao;

import java.util.List;

import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import br.com.caelum.vraptor.ioc.Component;
import br.com.extrajudicial.models.User;

@Component
public class UserDao extends GenericDao<User>{
	
	public User login(User user) {
		return (User) this.getSession().createCriteria(User.class)
			.add(Restrictions.eq("email", user.getEmail()))
			.add(Restrictions.eq("passwd", user.getPasswd()))
			.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<User> search(String nameOrEmail) {
		return this.getSession().createCriteria(User.class)
			.add(Restrictions.or(
					Restrictions.ilike("name", nameOrEmail, MatchMode.ANYWHERE), 
					Restrictions.ilike("email", nameOrEmail, MatchMode.ANYWHERE)))
			.list();
	}
	
}
